<template>
  <div class="wrapper">
    <!-- bidirectional data binding（双向数据绑定） -->
    <quill-editor
      ref="editor"
      content-type="html"
      v-model:content="content"
      :options="editorOption"
      @blur="editorBlur($event)"
    />
  </div>
</template>
<script setup lang="ts">
import { onMounted, ref, toRaw, watch } from "vue";
let editorOption = {
  modules: {
    toolbar: [
      ["bold", "italic", "underline", "strike"], // 加粗 斜体 下划线 删除线
      [{ color: [] }, { background: [] }], // 字体颜色、字体背景颜色
      [{ align: [] }], // 对齐方式
      [{ size: ["small", false, "large", "huge"] }], // 字体大小
      [{ font: [] }], // 字体种类
      [{ header: [1, 2, 3, 4, 5, 6, false] }], // 标题
      [{ direction: "ltl" }], // 文本方向
      [{ direction: "rtl" }], // 文本方向
      [{ indent: "-1" }, { indent: "+1" }], // 缩进
      [{ list: "ordered" }, { list: "bullet" }], // 有序、无序列表
      [{ script: "sub" }, { script: "super" }], // 上标/下标
      ["blockquote", "code-block"], // 引用  代码块
      ["clean"], // 清除文本格式
      //["link", "image", "video"], // 链接、图片、视频
    ],
  },
  placeholder: "请输入",
  theme: "snow",
};
const editor = ref();
const content = ref("");
function editorBlur(val: any) {
  console.log("content===" + content.value);
}
/**
 * if (res.data.url) {
        const quill = toRaw(myQuillEditor.value).getQuill()
        const length = quill.getSelection().index
        quill.insertEmbed(length, 'image', res.data.url)
        quill.setSelection(length + 1)
      }
 */
onMounted(() => {
  window["setContent"] = (result: string) => {
    setContent(result);
  };
  window["getContent"] = () => {
    return unescape(content.value);
  };
});
function setContent(con: string) {
  content.value = con;
  editor.value.setHTML(con);
  editor.value.getQuill().setSelection(con.length + 1);
}
</script>
<style scoped>
.wrapper {
  width: 100%;
  height: 100%;
  display: flex;
  flex-direction: column;
  box-sizing: border-box;
}
</style>
